package cn.edu.ctbu.demo1.service.impl;

import cn.edu.ctbu.demo1.domain.Student;
import cn.edu.ctbu.demo1.exception.REnum;
import cn.edu.ctbu.demo1.exception.RException;
import cn.edu.ctbu.demo1.repository.StudentRepository;
import cn.edu.ctbu.demo1.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class StudentImplService implements StudentService {

    @Autowired //帮我new一个对象 满足studentRepository这个接口  IOC
    private StudentRepository studentRepository; //studentRepository接口，不能new
    public List<Student> findAll(){
        return studentRepository.findAll();
    }

    /**
     * 只支持分页的查询
     * @param pageable
     * @return
     */
    public Page<Student> findAll(Pageable pageable){
        return studentRepository.findAll(pageable);
    }
    public List<Student> findAll(Example<Student> example){
        return studentRepository.findAll(example);
    }
    /**
    *按id进行查询
    * @param id 主键，整数
     * @return
    */
    public Student findById(Integer id){


        return  studentRepository.findById(id).orElse(null);
    }

    /**
     *按姓名进行查询 ,like
     * @param name 主键，整数
     * @return
     */
    public List<Student> findByName(String name){

        return  studentRepository.findByNameLike(name);
    }

    /**
     *按姓名和密码进行查询 ,like
     * @param name
     * @param password
     * @return
     */
    public List<Student> findByNameAndPassword(String name,String password){

        return  studentRepository.findByNameAndPassword(name,password);
    }
    public List<Student> findByNoAndPassword(String no,String password){
        return studentRepository.findByNoAndPassword(no, password);
    }

    public Student insert(Student student){
        studentRepository.save(student);
        return student;
    }
    public Student update(Student student){
        studentRepository.save(student);
        return student;
    }

    public void delete(Integer id){
//        Student student= new Student();
//        student.setId(id);
        studentRepository.deleteById(id);
    }

    /**
     * 分页，带过滤的查询
     * @param student
     * @param pageable
     * @return
     */
    public Page<Student> findAll(Example<Student> student,Pageable pageable){
        return  studentRepository.findAll(student,pageable);
    }

}
